package com.logbug.thread;

import java.util.concurrent.*;

public class TestPool {
    volatile static int runCounter=0;
    public static void main(String[] args) {

        // 当传入的队列不是SynchronousQueue时，超出核心线程数的任务将被放入队列，得不到执行，当超出队列容量时才创建新的线程去执行。
        // 同时当任务超过最大线程数时，会抛出异常
//        LinkedBlockingQueue queue = new LinkedBlockingQueue(1);
        BlockingQueue queue=new SynchronousQueue();

        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 4, 5, TimeUnit.SECONDS, queue);
        int num = 5;
        for (int i = 0; i < num; i++) {
            final int ti =  i+1;
            executor.execute(() -> {
                try {
                    while (true){
                        System.out.println("t" + ti + "...");
                        if ((++runCounter)%num==0){
                            System.out.println("================");
                        }
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }


    }
}
